<template>
  <div class="login-setting">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" type="password" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">更新</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getUserDetailById, saveUserDetailById } from '@/api/user'
export default {
  name: 'LoginSetting',
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 调用获取员工详情数据的方法
    this.getUserDetailById()
  },
  methods: {
    // 确认更新
    onSubmit() {
      this.$refs.form.validate(async valid => {
        if (!valid) return
        // 获取员工的 id
        const id = this.$route.query.id
        const res = await saveUserDetailById(id, this.form)

        // 更新成功以后，给用户提示
        this.$message.success(res.message)
        // 更新成功以后，需要获取最新的数据
        this.getUserDetailById()
      })
    },

    // 获取员工详情数据
    async getUserDetailById() {
      const res = await getUserDetailById(this.$route.query.id)
      this.form = res.data
    }
  }
}
</script>

<style lang="scss" scoped>
form.el-form {
  width: 500px;
  margin-top: 30px;
}
</style>
